
<table class='cheatsheet'>
<tr>
<td><h3>Creating Arrays</h3></td>
<td>

<h4>Lists</h4>

<pre>
$a = array(1, 2, 3, 4);	
$b = array("abc", "def", "ghi");
$empty = array();
</pre>

Assign values to specific indizes:

<pre>
$list = array();
$list[1] = 'Tom';
$list[45] = 'Sam';
$list[100] = 'Julie';
</pre>

<h4>Hashes</h4>

To create an associative array create a list of key/value pairs:

<pre>
$h = array(
	'command'	=> 'grep',
	'path'		=> '/usr/bin',
	'options'	=> '-ac',
	'param1'	=> '"pattern"',
	'param2'	=> '/data/file1.txt'
);
</pre>
</td>
</tr>
<tr>

<td><h3>Nesting Arrays</h3></td>
<td>

To create a multi-dimensional array just nest array() declarations as values:

<pre>
$a = array(
	array("key1", 4, 0, "description 1"),
	array("key2", 0, 33, ""),
	array("", 0, 10000, "something)
);
</pre>

</td>
</tr>

<tr><td><h3>Array &lt;-> String</h3></td>
<td>
See also: <?add topic='PHP implode'?> <?add topic='PHP explode'?>
<pre>$s = implode($a, ",");
$a = explode($s, ",");
</pre>

</td>

<tr><td><h3>Array Functions</h3></td>
<td>

<pre>
array_push($a, "new value");
array_unshift($a, "new value");

$value = array_pop($a);
$value = array_shift($a);

array_walk($a, 'callback_function');

$a = array_unique($a);

$keys    = array_keys($a);
$indices = array_keys($a, 'key value');
</pre>

</td>

<tr><td><h3>Iterating Arrays</h3></td>
<td>

<h4>Get Length</h4>

<pre>echo count($list);</pre>

<h4>List for</h4>

<pre>
for($i = 0; $i < count($list); $i++) {
	echo "$i: $list[$i]\n";
}
</pre>

<h4>List foreach</h4>

<pre>
foreach($list as $element) {
	print "$element\n";
}
</pre>

<h4>Hash foreach</h4>

<pre>
foreach($hash as $key => $value) {
	print "$key => $value\n";
}
</pre>

</td>
</tr>
</table>

